<template>
    <el-dialog v-model="visible" :title="titleMap[mode]" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form ref="dialogForm" :model="form" :rules="rules" :disabled="mode == 'show'" label-width="100px">

            <el-form-item label="映射部门" prop="name">
                <el-input v-model="form.name" placeholder="请输入部门名称" clearable :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="映射省份" prop="provinceIds">
                <el-select v-model="form.provinceIds" placeholder="请选择映射省份" style="width: 100%" filterable clearable
                    multiple :loading="provinceLoading">
                    <el-option v-for="item in provinceList" :key="item.id" :label="item.label"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="描述" prop="description">
                <el-input v-model="form.description" placeholder="请输入描述" clearable></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
export default {
    emits: ['success', 'closed'],
    data() {
        return {
            mode: 'edit',
            titleMap: {
                add: '新增',
                edit: '编辑',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
            //表单数据
            form: {
                id: 0,
                name: '',
                provinceDTOList: [],
                provinceIds: [],
                description: ''
            },
            //验证规则
            rules: {
                name: [{ required: true, message: '请输入部门名称' }],
                provinceIds: [{ required: true, message: '请选择映射省份' }]
            },
            provinceList: [],
            provinceLoading: false
        }
    },
    async mounted() {
        this.provinceLoading = true
        var res = await this.$API.emp.getChinaTree.post()
        if (res && res.code == 0) {
            this.provinceList = res.data.map(province => ({
                id: province.code,
                label: province.province,
                value: province.code + "/" + province.province
            }));
        }
        var mapRes = await this.$API.dept.deptToProvince.post({ data: { depId: this.form.id } })
        if (mapRes && mapRes.code == 0) {
            this.form.provinceIds = mapRes.data.map((item) => {
                return item.provinceId + '/' + item.provinceName
            })
        }
        this.provinceLoading = false
    },
    methods: {
        //显示
        open(mode = 'edit') {
            this.mode = mode
            this.visible = true
            return this
        },

        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true
                    // this.form.provinceDTOList = this.form.provinceIds
                    let code = ''
                    let name = ''
                    for (let item of this.form.provinceIds) {
                        code = item.split('/')[0]
                        name = item.split('/')[1]
                        this.form.provinceDTOList.push({
                            depId: 0,
                            provinceId: code,
                            provinceName: name,
                        })
                    }
                    // delete this.form.provinceIds
                    var res = await this.$API.dept.edit.post({ data: this.form })
                    this.isSaveing = false
                    if (res && res.code == 0) {
                        this.$emit('success', this.form, this.mode)
                        this.$message.success('操作成功')
                    }
                    this.visible = false
                }
            })
        },
        //表单注入数据
        setData(data) {
            // this.form.name = data.label
            // this.form.enable = data.enable
            // this.form.pid = data.pid
            // this.form.id = data.id
            // this.form.parentId = data.parentId
            // this.form.remark = data.remark

            //可以和上面一样单个注入，也可以像下面一样直接合并进去
            Object.assign(this.form, data)
        }
    }
}
</script>

<style></style>
